﻿using ResLibary;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//namespace ResLibary
//{
[CreateAssetMenu(fileName = "AssetLibarySetting", menuName = "LibaryAssetSetting")]
[System.Serializable]
public class LibaryAssetSetting : ScriptableObject
{
    public List<string> texture2dks;
    public List<string> renderTextureks;
    public List<string> spriteks;
    public List<string> textAssetks;
    public List<string> materialks;
    public List<string> prefabks;
    public List<string> audioks;
    public List<string> videoks;
    public List<string> movieTextureks;
    public List<string> fontks;
    public List<string> scriptableObjectks;
    public List<string> runtimeAnimatorControllerks;
    public List<UnityEngine.Texture2D> texture2ds;
    public List<UnityEngine.RenderTexture> renderTextures;
    public List<UnityEngine.Sprite> sprites;
    public List<UnityEngine.TextAsset> textAssets;
    public List<UnityEngine.Material> materials;
    public List<UnityEngine.GameObject> prefabs;
    public List<UnityEngine.AudioClip> audios;
    public List<UnityEngine.Video.VideoClip> videos;
    public List<UnityEngine.MovieTexture> movieTextures;
    public List<UnityEngine.Font> fonts;
    public List<UnityEngine.ScriptableObject> scriptableObjects;
    public List<UnityEngine.RuntimeAnimatorController> runtimeAnimatorControllers;
    public int settingId { get; private set; }


    private void Awake()
    {
        settingId = System.Guid.NewGuid().GetHashCode();
        if(texture2dks == null)
            texture2dks = new List<string>();
        if (texture2ds == null)
            texture2ds = new List<UnityEngine.Texture2D>();
        if (renderTextureks == null)
            renderTextureks = new List<string>();
        if (renderTextures == null)
            renderTextures = new List<UnityEngine.RenderTexture>();
        if (spriteks == null)
            spriteks = new List<string>();
        if (sprites == null)
            sprites = new List<UnityEngine.Sprite>();
        if (textAssetks == null)
            textAssetks = new List<string>();
        if (textAssets == null)
            textAssets = new List<UnityEngine.TextAsset>();
        if (materialks == null)
            materialks = new List<string>();
        if (materials == null)
            materials = new List<UnityEngine.Material>();
        if (prefabks == null)
            prefabks = new List<string>();
        if (prefabs == null)
            prefabs = new List<UnityEngine.GameObject>();
        if (audioks == null)
            audioks = new List<string>();
        if (audios == null)
            audios = new List<UnityEngine.AudioClip>();
        if (videoks == null)
            videoks = new List<string>();
        if (videos == null)
            videos = new List<UnityEngine.Video.VideoClip>();
        if (movieTextureks == null)
            movieTextureks = new List<string>();
        if (movieTextures == null)
            movieTextures = new List<UnityEngine.MovieTexture>();
        if (fontks == null)
            fontks = new List<string>();
        if (fonts == null)
            fonts = new List<UnityEngine.Font>();
        if (scriptableObjectks == null)
            scriptableObjectks = new List<string>();
        if (scriptableObjects == null)
            scriptableObjects = new List<UnityEngine.ScriptableObject>();

        if (runtimeAnimatorControllerks == null)
            runtimeAnimatorControllerks = new List<string>();
        if (runtimeAnimatorControllers == null)
            runtimeAnimatorControllers = new List<UnityEngine.RuntimeAnimatorController>();
    }

    public Dictionary<string, List<string>> GetAssetDict()
    {
        Dictionary<string, List<string>> Dict = new Dictionary<string, List<string>>();
        Dict[ResLibaryConfig.ExistType[LibaryTypeEnum.LibaryType_Texture2D]] = new List<string>(texture2dks); //GetAssetList(texture2ds);
        Dict[ResLibaryConfig.ExistType[LibaryTypeEnum.LibaryType_RenderTexture]] = new List<string>(renderTextureks); //GetAssetList(renderTextures);
        Dict[ResLibaryConfig.ExistType[LibaryTypeEnum.LibaryType_Sprite]] = new List<string>(spriteks); //GetAssetList(sprites);
        Dict[ResLibaryConfig.ExistType[LibaryTypeEnum.LibaryType_TextAsset]] = new List<string>(textAssetks); //GetAssetList(textAssets);
        Dict[ResLibaryConfig.ExistType[LibaryTypeEnum.LibaryType_Material]] = new List<string>(materialks); //GetAssetList(materials);
        Dict[ResLibaryConfig.ExistType[LibaryTypeEnum.LibaryType_GameObject]] = new List<string>(prefabks); //GetAssetList(prefabs);
        Dict[ResLibaryConfig.ExistType[LibaryTypeEnum.LibaryType_AudioClip]] = new List<string>(audioks); //GetAssetList(audios);
        Dict[ResLibaryConfig.ExistType[LibaryTypeEnum.LibaryType_VideoClip]] = new List<string>(videoks); //GetAssetList(videos);
        Dict[ResLibaryConfig.ExistType[LibaryTypeEnum.LibaryType_MovieTexture]] = new List<string>(movieTextureks); // GetAssetList(movieTextures);
        Dict[ResLibaryConfig.ExistType[LibaryTypeEnum.LibaryType_Font]] = new List<string>(fontks);
        Dict[ResLibaryConfig.ExistType[LibaryTypeEnum.LibaryType_ScriptableObject]] = new List<string>(scriptableObjectks);
        Dict[ResLibaryConfig.ExistType[LibaryTypeEnum.LibaryType_RuntimeAnimatorController]] = new List<string>(runtimeAnimatorControllerks);
        return Dict;
    }
    public List<string> GetAssetList<T>(List<T> dist) where T : UnityEngine.Object
    {
        List<string> list = new List<string>();
        T[] ts = dist == null ? new T[0] : dist.ToArray();
        for (int i = 0; i < ts.Length; i++)
        {
            T t = ts[i];
            if (t != null && !list.Contains(t.name))
            {
                list.Add(t.name);
            }
        }
        return list;
    }
    public void AddResToLibary<T>(T t) where T : UnityEngine.Object
    {
        string m_Type ="";
        ResLibaryConfig.TryGetTypeName(t,out m_Type);
        LibaryTypeEnum libaryStatusEnum;
        if (!ResLibaryConfig.ExistTypeNameToEnum.TryGetValue(m_Type, out libaryStatusEnum))
            return;
        UnityEngine.Object data = t;
        switch (libaryStatusEnum)
        {
            case LibaryTypeEnum.LibaryType_Texture2D:
                if (!texture2dks.Contains(data.name))
                {
                    texture2dks.Add(data.name);
                    texture2ds.Add((UnityEngine.Texture2D)data);
                }
                break;
            case LibaryTypeEnum.LibaryType_RenderTexture:
                if (!renderTextureks.Contains(data.name))
                {
                    renderTextureks.Add(data.name);
                    renderTextures.Add((UnityEngine.RenderTexture)data);
                }

                break;
            case LibaryTypeEnum.LibaryType_MovieTexture:
                if (!movieTextureks.Contains(data.name))
                {
                    movieTextureks.Add(data.name);
                    movieTextures.Add((UnityEngine.MovieTexture)data);
                }

                break;
            case LibaryTypeEnum.LibaryType_Sprite:
                if (!spriteks.Contains(data.name))
                {
                    spriteks.Add(data.name);
                    sprites.Add((UnityEngine.Sprite)data);
                }
                break;
            case LibaryTypeEnum.LibaryType_TextAsset:
                if (!textAssetks.Contains(data.name))
                {
                    textAssetks.Add(data.name);
                    textAssets.Add((UnityEngine.TextAsset)data);
                }
                break;
            case LibaryTypeEnum.LibaryType_Material:
                if (!materialks.Contains(data.name))
                {
                    materialks.Add(data.name);
                    materials.Add((UnityEngine.Material)data);
                }
                break;
            case LibaryTypeEnum.LibaryType_GameObject:
                if (!prefabks.Contains(data.name))
                {
                    prefabks.Add(data.name);
                    prefabs.Add((UnityEngine.GameObject)data);
                }
                break;
            case LibaryTypeEnum.LibaryType_AudioClip:
                if (!audioks.Contains(data.name))
                {
                    audioks.Add(data.name);
                    audios.Add((UnityEngine.AudioClip)data);
                }
                break;
            case LibaryTypeEnum.LibaryType_VideoClip:
                if (!videoks.Contains(data.name))
                {
                    videoks.Add(data.name);
                    videos.Add((UnityEngine.Video.VideoClip)data);
                }
                break;
            case LibaryTypeEnum.LibaryType_Font:
                if (!fontks.Contains(data.name))
                {
                    fontks.Add(data.name);
                    fonts.Add((UnityEngine.Font)data);
                }
                break;
            case LibaryTypeEnum.LibaryType_ScriptableObject:
                if (!scriptableObjectks.Contains(data.name))
                {
                    scriptableObjectks.Add(data.name);
                    scriptableObjects.Add((UnityEngine.ScriptableObject)data);
                }
                break;
            case LibaryTypeEnum.LibaryType_RuntimeAnimatorController:
                if (!runtimeAnimatorControllerks.Contains(data.name))
                {
                    runtimeAnimatorControllerks.Add(data.name);
                    runtimeAnimatorControllers.Add((UnityEngine.RuntimeAnimatorController)data);
                }
                break;
        }
    }


    public void DelResToLibary<T>(T t) where T : UnityEngine.Object
    {
        if (t == null)
            return;
        if (t is UnityEngine.Texture2D)
        {
            DelResToLibary(ResLibaryConfig.ExistType[LibaryTypeEnum.LibaryType_Texture2D],t.name);
        }
        else if (t is UnityEngine.RenderTexture)
        {
            DelResToLibary(ResLibaryConfig.ExistType[LibaryTypeEnum.LibaryType_RenderTexture], t.name);
        }
        else if (t is UnityEngine.MovieTexture)
        {
            DelResToLibary(ResLibaryConfig.ExistType[LibaryTypeEnum.LibaryType_MovieTexture], t.name);
        }
        else if (t is UnityEngine.Sprite)
        {
            DelResToLibary(ResLibaryConfig.ExistType[LibaryTypeEnum.LibaryType_Sprite], t.name);
        }
        else if (t is UnityEngine.TextAsset)
        {
            DelResToLibary(ResLibaryConfig.ExistType[LibaryTypeEnum.LibaryType_TextAsset], t.name);
        }
        else if (t is UnityEngine.Material)
        {
            DelResToLibary(ResLibaryConfig.ExistType[LibaryTypeEnum.LibaryType_Material], t.name);
        }
        else if (t is UnityEngine.GameObject)
        {
            DelResToLibary(ResLibaryConfig.ExistType[LibaryTypeEnum.LibaryType_GameObject], t.name);
        }
        else if (t is UnityEngine.AudioClip)
        {
            DelResToLibary(ResLibaryConfig.ExistType[LibaryTypeEnum.LibaryType_AudioClip], t.name);
        }
        else if (t is UnityEngine.Video.VideoClip)
        {
            DelResToLibary(ResLibaryConfig.ExistType[LibaryTypeEnum.LibaryType_VideoClip], t.name);
        }
        else if (t is UnityEngine.Font)
        {
            DelResToLibary(ResLibaryConfig.ExistType[LibaryTypeEnum.LibaryType_Font], t.name);
        }
        else if (t is UnityEngine.ScriptableObject)
        {
            DelResToLibary(ResLibaryConfig.ExistType[LibaryTypeEnum.LibaryType_ScriptableObject], t.name);
        }
        else if (t is UnityEngine.RuntimeAnimatorController)
        {
            DelResToLibary(ResLibaryConfig.ExistType[LibaryTypeEnum.LibaryType_RuntimeAnimatorController], t.name);
        }
    }

    public void DelResToLibary(string _type, string objName)
    {
        LibaryTypeEnum libaryStatusEnum;
        string m_Type = _type;
        if (!ResLibaryConfig.ExistTypeNameToEnum.TryGetValue(m_Type, out libaryStatusEnum))
            return;
        int resIndex = -1;
        switch (libaryStatusEnum)
        {
            case LibaryTypeEnum.LibaryType_Texture2D:
                resIndex = texture2dks.FindIndex((obj)=> { return obj == objName; });
                if (resIndex != -1)
                {
                    texture2dks.RemoveAt(resIndex);
                    texture2ds.RemoveAt(resIndex);
                }
                break;
            case LibaryTypeEnum.LibaryType_RenderTexture:
                resIndex = renderTextureks.FindIndex((obj) => { return obj == objName; });
                if (resIndex != -1)
                {
                    renderTextureks.RemoveAt(resIndex);
                    renderTextures.RemoveAt(resIndex);
                }
                break;
            case LibaryTypeEnum.LibaryType_MovieTexture:
                resIndex = movieTextureks.FindIndex((obj) => { return obj == objName; });
                if (resIndex != -1)
                {
                    movieTextureks.RemoveAt(resIndex);
                    movieTextures.RemoveAt(resIndex);
                }
                break;
            case LibaryTypeEnum.LibaryType_Sprite:
                resIndex = spriteks.FindIndex((obj) => { return obj == objName; });
                if (resIndex != -1)
                {
                    spriteks.RemoveAt(resIndex);
                    sprites.RemoveAt(resIndex);
                }
                break;
            case LibaryTypeEnum.LibaryType_TextAsset:
                resIndex = textAssetks.FindIndex((obj) => { return obj == objName; });
                if (resIndex != -1)
                {
                    textAssetks.RemoveAt(resIndex);
                    textAssets.RemoveAt(resIndex);
                }
                break;
            case LibaryTypeEnum.LibaryType_Material:
                resIndex = materialks.FindIndex((obj) => { return obj == objName; });
                if (resIndex != -1)
                {
                    materialks.RemoveAt(resIndex);
                    materials.RemoveAt(resIndex);
                }
                break;
            case LibaryTypeEnum.LibaryType_GameObject:
                resIndex = prefabks.FindIndex((obj) => { return obj == objName; });
                if (resIndex != -1)
                {
                    prefabks.RemoveAt(resIndex);
                    prefabs.RemoveAt(resIndex);
                }
                break;
            case LibaryTypeEnum.LibaryType_AudioClip:
                resIndex = audioks.FindIndex((obj) => { return obj == objName; });
                if (resIndex != -1)
                {
                    audioks.RemoveAt(resIndex);
                    audios.RemoveAt(resIndex);
                }
                break;
            case LibaryTypeEnum.LibaryType_VideoClip:
                resIndex = videoks.FindIndex((obj) => { return obj == objName; });
                if (resIndex != -1)
                {
                    videoks.RemoveAt(resIndex);
                    videos.RemoveAt(resIndex);
                }
                break;
            case LibaryTypeEnum.LibaryType_Font:
                resIndex = fontks.FindIndex((obj) => { return obj == objName; });
                if (resIndex != -1)
                {
                    fontks.RemoveAt(resIndex);
                    fonts.RemoveAt(resIndex);
                }
                break;
            case LibaryTypeEnum.LibaryType_ScriptableObject:
                resIndex = scriptableObjectks.FindIndex((obj) => { return obj == objName; });
                if (resIndex != -1)
                {
                    scriptableObjectks.RemoveAt(resIndex);
                    scriptableObjects.RemoveAt(resIndex);
                }
                break;
        }
    }
}
//}



